/*
大厅界面中的管理员功能界面，主要是给玩家开包厢，关包厢，开启关闭维护等
*/
var confige = require("confige");

cc.Class({
    extends: cc.Component,

    properties: {
        roomItem:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.roomListView = this.node.getChildByName("roomList");
        this.roomListContent = this.roomListView.getChildByName("view").getChildByName("content");
        this.roomItemBeginY = 40;
        this.roomItemOffsetY = 80;
        this.roomItemList = {};

        this.addRoomLayer = this.node.getChildByName("addRoomLayer");
        this.addRoomId = this.addRoomLayer.getChildByName("idEdit").getComponent("cc.EditBox");

        this.tipsBox = this.node.getChildByName("tipsBox");
        this.btnRepair = this.node.getChildByName("btnRepair");
        this.btnRepairClose = this.node.getChildByName("btnRepairClose");
        this.tipsBox1 = this.node.getChildByName("tipsBox1");
        if(confige.userInfo.limits >= 5){
            this.btnRepair.active = true;
            this.btnRepairClose.active = true;
        }
        this.isInit = true;
    },

    updateLayer:function(roomData){
        for(var i in this.roomItemList)
        {
            this.roomItemList[i].destroy();
        }
        this.roomItemList = {};

        var roomItemCount = 0;
        var newRoomItemY = this.roomItemBeginY;
        for(var i in roomData)
        {
            roomItemCount++;
            var newRoomItem = cc.instantiate(this.roomItem);
            newRoomItem.y = -newRoomItemY;
            this.roomItemList[i] = newRoomItem;
            this.roomListContent.addChild(newRoomItem);
            newRoomItem.active = true;
            newRoomItemY += this.roomItemOffsetY;

            var self = this;
            var btnRemove = newRoomItem.getChildByName("remove").getComponent("cc.Button");
            var removeCallBack = function(){
                console.log("removeCallBack!!!!!!!!!!!");
                self.removeRoom(removeCallBack.roomID);
            };
            removeCallBack.roomID = i;
            btnRemove.node.on(cc.Node.EventType.TOUCH_END, removeCallBack, self);

            var roomID = newRoomItem.getChildByName("roomID").getComponent("cc.Label");
            roomID.string = i;
            var bankerID = newRoomItem.getChildByName("bankerID").getComponent("cc.Label");
            bankerID.string = roomData[i].uid;
            var nickName = newRoomItem.getChildByName("nickName").getComponent("cc.Label");
            nickName.string = roomData[i].nickname;
            var openTime = newRoomItem.getChildByName("openTime").getComponent("cc.Label");
            openTime.string = roomData[i].runCount;
            var canUseTime = newRoomItem.getChildByName("canUseTime").getComponent("cc.Label");
            canUseTime.string = roomData[i].diamnd;
        }
        if(newRoomItemY < 400)
            newRoomItemY = 400;
        this.roomListContent.height = newRoomItemY + 100;
    },
    
    removeRoom:function(roomID){
        this.tipsBox.active = true;
        this.curRemoveID = roomID;
    },

    tipsBoxOK:function(){
        var self = this;
        console.log("removeRoom" + this.curRemoveID);
        pomelo.request("connector.entryHandler.sendData", {"code" : "superOperation","params" : {"cmd" : "closeBalcony","roomId" : parseInt(this.curRemoveID)}}, function(data) {
            console.log("closeBalcony flag is : ");
            console.log(data);
            self.refreshList();
            self.tipsBox.active = false;
        });
    },

    tipsBoxCancel:function(){
        this.tipsBox.active = false;
    },

    btnRepairClick:function(){
        this.tipsBox1.active = true;
    },
    tips1BoxOK:function(){
        var self = this;
        pomelo.request("connector.entryHandler.sendData", {"code" : "superOperation","params" : {"cmd" : "beginMaintain"}}, function(data) {
            console.log("beginMaintain flag is : ");
            console.log(data);
            if(data.flag)
                window.clientGlobal.showTipsWord("成功开启维护!");
            self.tipsBox1.active = false;
        });
    },
    btnRepairCloseClick:function(){
        var self = this;
        pomelo.request("connector.entryHandler.sendData", {"code" : "superOperation","params" : {"cmd" : "stopMaintain"}}, function(data) {
            console.log("stopMaintain flag is : ");
            console.log(data);
            if(data.flag)
                window.clientGlobal.showTipsWord("成功关闭维护!");
        });
    },
    tips1BoxCancel:function(){
        this.tipsBox1.active = false;
    },

    showAddRoomLayer:function(){
        this.addRoomLayer.active = true;
    },

    hideAddRoomLayer:function(){
        this.addRoomLayer.active = false;
    },

    btnAddRoomClick:function(){
        var self = this;
        pomelo.request("connector.entryHandler.sendData", {"code" : "superOperation","params" : {"cmd" : "openBalcony","gameType" : "PaiGowBalcony","uid" : parseInt(this.addRoomId.string)}}, function(data) {
            console.log("openBalcony flag is : ")
            console.log(data);
            self.refreshList();
        });
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.refreshList();
        this.node.active = true;
    },

    hideLayer:function(){
        this.node.active = false;
    },

    refreshList:function(){
        var self = this;
        pomelo.request("connector.entryHandler.sendData", {"code" : "superOperation","params" : {"cmd" : "getBalconyList"}}, function(data) {
            console.log("getBalconyList flag is : ")
            console.log(data);
            self.updateLayer(data.flag);
        });
    },
});
